//
//  KeybordViewController.m
//  oc_demo
//
//  Created by haigui on 2021/10/12.
//

#import "KeybordViewController.h"

@interface KeybordViewController ()

@property (nonatomic, strong) UIView *bottomView;

@end

@implementation KeybordViewController

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    [self.view addSubview:self.bottomView];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    UITextField *tf = [_bottomView viewWithTag:100];
    if ([tf canBecomeFirstResponder]) {
        [tf becomeFirstResponder];
    }
}

- (void)keyboardWillShow:(NSNotification*)noti{
    CGSize size = [[noti.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame = _bottomView.frame;
        frame.origin.y = SCREEN_HEIGHT-NavigationBarHeight-(size.height+_bottomView.height);
        _bottomView.frame = frame;
    }];
}
- (void)keyboardWillHide:(NSNotification*)noti{
    
    _bottomView.y = SCREEN_HEIGHT-NavigationBarHeight-_bottomView.height;
}


- (UIView *)bottomView{
    if (!_bottomView) {
        _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-NavigationBarHeight-55-BottomSpace, SCREEN_WIDTH, 55+BottomSpace)];
        _bottomView.backgroundColor = [UIColor redColor];
        
        UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(10, 10, _bottomView.width-20, 35)];
        tf.backgroundColor = [UIColor orangeColor];
        tf.tag = 100;
        [_bottomView addSubview:tf];
    }
    return _bottomView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
